package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.baselibrary.base.NoDataViewHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.BuildingCountBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/14 10:44
 */

public class BuildingCountAdapter extends RecyclerView.Adapter<BaseHolder> {

    private Context context;
    private List<BuildingCountBean> data;

    public BuildingCountAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (data == null) {
            return -1;
        } else if (data.size() == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.lv_empty_view, parent, false));
        } else if (viewType == 1) {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_building_count, parent, false));
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        holder.instanceView(position);
    }

    @Override
    public int getItemCount() {
        if (data == null) {
            return 0;
        } else if (data.size() == 0) {
            return 1;
        } else {
            return data.size();
        }
    }

    public class ViewHolder extends BaseHolder {

        private TextView tv_building_name;
        private TextView tv_building_count;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void assignViews() {
            tv_building_name = (TextView) findViewById(R.id.tv_building_name);
            tv_building_count = (TextView) findViewById(R.id.tv_building_count);

        }

        @Override
        public void instanceView(int position) {
            if (data != null) {
                BuildingCountBean buildingCountBean = data.get(position);
                tv_building_name.setText(buildingCountBean.getHouse_name());
                tv_building_count.setText(buildingCountBean.getUser_num() + "人");
            }
        }
    }

    public void notifyData(List<BuildingCountBean> list) {
        if (data == null) {
            data = new ArrayList<>();
        }
        if (list != null && list.size() > 0) {
            this.data.clear();
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }
}
